<?php
namespace app\api\controller;

use app\common\controller\ApiController;
use think\App;
use \app\admin\traits\Curd;
use think\facade\Db;
use app\api\controller\Adv;
use app\api\model\User as ModelUser;
use PhpOffice\PhpSpreadsheet\Reader\Xls\MD5;

class Shop extends ApiController
{
    protected $noNeedLogin = [
        "get_banner_list",
        "get_classification_list",
        "recommend_service",
        "test"
        
    ];
    protected $mustLogin = [];
    protected $counter = 0;

    use \app\admin\traits\Curd;
    public function __construct(App $app)
    {
        parent::__construct($app);
    }
    //轮播图列表
    public function get_banner_list()
    {
        $param = $this->request->param();
        $num = isset($param['num'])?$param['num']:5;
        $list = Db::name("banner")->order('id desc')->paginate($num);
        $this->success('获取成功', $list);
    }

  
    //导航列表
    public function get_navigation_list(){
        $list = Db::name("classification")->order('id desc')->paginate(10);
        $list = $list->toArray();
        $this->success('获取成功', $list);
    }

    //绑定账号 关联crm客户唯一ID
    public function crmcoutest_userid(){
        $param = $this->request->param();
        if(empty($param["phone"])){
            $this->error('手机号不能为空');
        }
       // $tel = "18292240502";//18292240502  18192572972
        //$user = Db::name("user")->where("phone",$param["phone"])->find();
        $gl_customer = $this->gl_phone_customer($param["phone"]);//客户
        if(empty($gl_customer)){
            $this->error("客户手机号不存在，关联失败");
        }
        Db::name("user")->where("phone",$param["phone"])->update(["crm_dataid"=>$gl_customer]);
  
        $this->success('关联成功！');
    
    }

     //接收crm方案id
     public function receive_crmplan_id(){
        $param = $this->request->param();
        if(empty($param["crmplan_id"])){
            $this->error("方案id不能为空");
        }
        if(empty($param["customer_id"])){
            $this->error("客户id不能为空");
        }

        $flag = Db::name("user")->where("crm_dataid",$param["customer_id"])->find();
        if(!empty($flag)){
            Db::name("user")->where("crm_dataid",$param["customer_id"])->update(["crmplan_id"=>$param["crmplan_id"]]);
        }
        return 1;
    }


      //获取个人方案
      public function get_personal_programmes(){
        $user = Db::name("user")->where("id",$this->uid)->find();
        if(empty($user["crmplan_id"])){
            $this->error('方案未同步，请同步方案');
        }
        // $scheme_id = [
        //     "3FO40H1NUG7A",
        //     "3FO40G9IFE2M"
        // ];
        $scheme_id = [
            //"3FO4BEUR3B4W"
            $user["crmplan_id"]
        ];
        $list = [];
        foreach($scheme_id as $k=>$v){
            //获取方案效果图
            $scheme_map = $this->get_rendering_list($v);
            if(empty($scheme_map["data"])){
                $scheme_map = [];
            }else{
                $scheme_map_data = $scheme_map["data"];
                $scheme_map = array_column($scheme_map_data,"img");
            }
            //获取方案详情
            $scheme_details = $this->get_scheme_details($v);
          
            if(empty($scheme_details["data"])){
                $scheme_details =[];
            }else{
                $scheme_details =$scheme_details["data"];
            }
            $resdata = [
                "scheme_map" =>$scheme_map,//方案效果图
                "scheme_details"=>$scheme_details,//户型图
            ];
            array_push($list,$resdata);
            
        }
        $this->success('获取成功', $list);

    }

      //获取方案详情
      public function get_scheme_details($design_id){
        if(empty($design_id)){
             $this->error('方案id不能为空');
        }
        $time = time()*1000;
        $pinsgin = "JUoUayxI1x1dDCVHPAtz4N4Y3Dqdzqid"."HoKTCYRyF6".$time;
        $sing = md5($pinsgin);
        $method = 'GET';
        $url = 'https://openapi.kujiale.com/v2/design/'.$design_id.'/basic?appkey=HoKTCYRyF6&timestamp='.$time.'&sign='.$sing;
     
        $header = [
                    'Content-Type:text/plain;charset=utf-8'
                  ];
        //$res =  json_encode($qwe);
        $res = "";
        $data = $this->http_curl($url,$method, $res, $header, $timeout = 10);
       if(!empty($data) && isset($data) && $data['c'] ==0)
       {
            $resu = [
                "code"=>1,
                "data"=>$data["d"]
            ];
       }else{
            $resu = [
                "code"=>-1,
                "data"=>""
            ];
       }
       return $resu;
    }

    //获取方案的渲染图
    public function get_rendering_list($design_id){
        if(empty($design_id)){
             $this->error('方案id不能为空');
        }
        $time = time()*1000;
        $pinsgin = "JUoUayxI1x1dDCVHPAtz4N4Y3Dqdzqid"."HoKTCYRyF6".$time;
        $sing = md5($pinsgin);
        $method = 'GET';
        $url = 'https://openapi.kujiale.com/v2/renderpic/list?appkey=HoKTCYRyF6&timestamp='.$time.'&sign='.$sing.'&design_id='.$design_id.'&start=0&num=10';

        $header = [
                    'Content-Type:text/plain;charset=utf-8'
                  ];
        //$res =  json_encode($qwe);
        $res = "";
        $data = $this->http_curl($url,$method, $res, $header, $timeout = 10);
       if(!empty($data) && isset($data) && $data['c'] ==0)
       {
            $resu = [
                "code"=>1,
                "data"=>$data["d"]["result"]
            ];
       }else{
            $resu = [
                "code"=>-1,
                "data"=>""
            ];
       }
       return $resu;
    }







    
 

     

    
 
   
}
